package com.aaa.docter.service.Impl;

import com.aaa.docter.Fegin.MedicinaFeign;
import com.aaa.docter.Fegin.UserFeign;
import com.aaa.docter.dao.CareOrderDao;
import com.aaa.docter.dao.CareOrderItemDao;
import com.aaa.docter.service.CareOrderService;
import com.aaa.docter.vo.CareOrderVo;
import com.aaa.entity.CareOrder;
import com.aaa.entity.CareOrderItem;
import com.aaa.entity.User;
import com.aaa.util.IdWorker;
import com.aaa.util.JwtUtil;
import com.aaa.util.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class CareOrderServiceImpl implements CareOrderService {
    @Autowired
    private CareOrderDao careOrderDao;

    @Autowired
    private CareOrderItemDao careOrderItemDao;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private MedicinaFeign medicinaFeign;

//        查询药品处方信息
    @Override
    public Result<List<CareOrder>> queryCareOrdersByChId(String chId) {
//        查询药品处方信息
        QueryWrapper<CareOrder> wrapper = new QueryWrapper<>();
        if (Objects.nonNull(chId)){
            wrapper.eq("ch_id",chId);
        }
        List<CareOrder> careOrders = careOrderDao.selectList(wrapper);
        System.out.println("careOrders ============================ " + careOrders);
//        遍历处方信息
        for (CareOrder careOrder : careOrders) {
            QueryWrapper<CareOrderItem> wrapper1 = new QueryWrapper<>();
            if (Objects.nonNull(careOrder.getCoId())){
//查询检查处方信息
                wrapper1.eq("co_id",careOrder.getCoId());
                List<CareOrderItem> careOrderItems = careOrderItemDao.selectList(wrapper1);
                careOrder.setCareOrderItemList(careOrderItems);
            }
        }

        return new Result<>(2000,"查询处方ok",careOrders);
    }



    @Override
    public Result<String> saveCareOrderItem(CareOrderVo careOrderVo) {
//        获取药
        CareOrder careOrder = careOrderVo.getCareOrder();
//        检查
        List<CareOrderItem> careOrderItems = careOrderVo.getCareOrderItems();
        //        获取token
        HttpServletRequest request = WebUtil.getRequest();
        String phone = (String) JwtUtil.getTokenChaim(request.getHeader("token")).get("username");


//---------------
        Long userId = userFeign.queryUserByPhone(Long.valueOf(phone)).getUserId();//用户id
        String userName = userFeign.queryUserByPhone(Long.valueOf(phone)).getUserName();
//---------------


        IdWorker worker = new IdWorker();
        long l = worker.nextId();
        String coId ="CO"+ l;//生成coId 存到his_care_order表中
        System.out.println("coId ========================= " + coId);
        careOrder.setCoId(coId);
        careOrder.setUserId(userId);
//        LocalDateTime now = LocalDateTime.now();
        careOrder.setCreateTime( new Date());
        careOrder.setCreateBy(userName);
        careOrderDao.insert(careOrder);
        for (CareOrderItem careOrderItem:careOrderItems){
            IdWorker worker2 = new IdWorker();
            long l2 = worker2.nextId();
            String ItemId ="ITEM"+ l2;
            System.out.println("ItemId ================== " + ItemId);
            careOrderItem.setItemId(ItemId);
            careOrderItem.setCoId(coId);
            careOrderItem.setStatus("0");
            careOrderItem.setCreateTime(new Date());
            careOrderItemDao.insert(careOrderItem);
        }

        return new Result<>(2000,"成功");
    }

//    处方删除
    @Override
    public Result<String> deleteCareOrderItemById(String itemId, BigDecimal amount) {
        QueryWrapper<CareOrderItem> wrapper=new QueryWrapper<>();
        wrapper.eq("item_id",itemId);
        CareOrderItem careOrderItem = careOrderItemDao.selectOne(wrapper);
        QueryWrapper<CareOrder> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("co_id",careOrderItem.getCoId());
        CareOrder careOrder = careOrderDao.selectOne(wrapper1);
        careOrder.setAllAmount(careOrder.getAllAmount().subtract(amount));//减去删除的金额
        careOrderDao.updateById(careOrder);
        if (careOrder.getAllAmount().intValue() == 0){
            careOrderDao.delete(wrapper1);
        }
        careOrderItemDao.delete(wrapper);
        return new Result<>(2000,"删除药品及检查详情成功");
    }
}
